Thread: array[0] != array + 0 ???

  1. #16
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Elysia View Post
    It will return 23 since &array[1] is the address starting at the second element, which is the "2".
    Atoi will the scan until it finds the end of the string, which is after the "3". So it will return 23.
    any way to atoi only give '2' to me instead of '23'? (without placing \0 after 2)


    Thanks.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    But as soon as you USE the pointer produced by camdados_dados + bk, you are in an equally "undefined behaviour" situation.
    That is not true, assuming that the use is restricted to pointer arithmetic and comparison.

    any way to atoi only give '2' to me instead of '23'? (without placing \0 after 2)
    In that case you may not want to use atoi() at all, but instead just array[0], or array[0] - '0' if you want the integer corresponding to that digit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by Milhas View Post
    any way to atoi only give '2' to me instead of '23'? (without placing \0 after 2)


    Thanks.
    No, not unless you truncate the array by replacing the '3' with 0. This is a typical way of getting only the data you want. You can simply make a copy of the current value at array[2], then replacing it with 0, calling atoi and then restoring the previous value.

    Also note that laserlight's suggestion will work on individual digits, but if it's a long string in question, the truncate option may work better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You should really not use atoi(), use strtol og strtoi instead...
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #20
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Thanks!

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Milhas View Post
    If i have char array[3] = "123"

    and give atoi(&array[1]), atoi will give me int '2' or int '23' ?
    There's no telling what atoi() will give you because your array isn't large enough to hold the string "123" with a null-terminator.

    Without that null-terminator on the end, atoi() will plow right past the end of your string and process whatever it finds. In most cases these probably won't be valid ASCII digits and you might get the correct result, but I wouldn't rely on that.

    If you're going to use a literal string in your program, don't specify the size of the array and let the compiler figure it out for you (char array[] = "123", sizeof(array) == 4).

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Actually, the code wouldn't compile in the first place, and if you manually copied the string into the buffer, you'd get a buffer overrun, perhaps a crash, but otherwise the NULL char would be there, and it would still work right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Elysia
    Actually, the code wouldn't compile in the first place, and if you manually copied the string into the buffer, you'd get a buffer overrun, perhaps a crash, but otherwise the NULL char would be there, and it would still work right.
    Interesting considering I just compiled it. I think perhaps you meant to say, "In Visual Studio, the code won't compile in the first place," because MinGW allows this with no errors even with -Wall and -pedantic. I assume gcc in general probably behaves the same way.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    No, according to C. If the compiler compiles it, the compiler is faulty.
    char[4] can't be implicitly converted to char[3].
    (Don't quote me on that.)
    (Btw, it's likely Visual Studio would compile it, as well, due to its poor C compiler.)
    Last edited by Elysia; 03-31-2008 at 10:30 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Elysia View Post
    No, according to C. If the compiler compiles it, the compiler is faulty.
    char[4] can't be implicitly converted to char[3].
    (Btw, it's likely Visual Studio would compile it, as well, due to its poor C compiler.)
    You might find 6.7.8p32 of ISO/IEC 9899:TC2 to be of interest.
    EXAMPLE 8 The declaration
    Code:
    char s[] = "abc", t[3] = "abc";
    defines ‘‘plain’’ char array objects s and t whose elements are initialized with character string literals.
    This declaration is identical to
    Code:
    char s[] = { 'a', 'b', 'c', '\0' },
         t[] = { 'a', 'b', 'c' };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Pfft again, stupid C
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Okay... http://c-faq.com/ansi/nonstrings.html

    -- Edit --
    Well then. Two posts above mine huh? Where did THOSE come from!?

    -- Double Edit --
    Interestingly enough, the code does not compile in Visual Studio. It complains of a buffer overrun. Hehe.
    Last edited by arpsmack; 03-31-2008 at 10:33 AM.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    I was wrong. Again, C surprises me with all the stupid things it allows.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM